const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
// const CopyPlugin = require("copy-webpack-plugin")

module.exports = {
  mode: 'development', //production

  devtool: 'source-map',

  // 入口
  entry: {
    app: './src/index.js'
  },

  // 出口
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'scripts/[name]-[hash:6].js'
  },

  // resolve
  resolve: {
    extensions: ['.vue', '.mjs', '.js', '.json']
  },

  // loader
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.(css|less)$/,
        // loader的执行顺序是从右往左，从下到上
        // use: [MiniCssExtractPlugin.loader, 'css-loader']
        use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'less-loader'
          }
        ]
      },
      {
        test: /\.(jpg|jpeg|png|gif)$/,
        use: [
          {
            loader: 'url-loader'
          }
        ]
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },

  // 插件
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './public/index.html'
    }),

    new MiniCssExtractPlugin({
      filename({chunk: { name }}) {
        return `styles/${name}-[hash:6].css`
      }
    }),

    new CleanWebpackPlugin(),

    new VueLoaderPlugin(),

    new CssMinimizerPlugin(),

    new CopyWebpackPlugin([
      {
        from: './public/data.json', 
        to: './public'
      }
    ])
  ],

  // devserver
  devServer: {
    contentBase: './dist',
    compress: true,
    port: 9000
  }
}